package com.hsq.reflection.testReflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectAccessMethod {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class<?> bossClass = Class.forName("com.hsq.reflection.testReflection.Boss");
        Object o = bossClass.newInstance();
        //get public
//        Method method = bossClass.getMethod("hi", String.class);
        //因为获取的是public，所以使用和getMethod一样
        Method hi = bossClass.getDeclaredMethod("hi", String.class);
        System.out.println(hi.invoke(o, "Java"));
        //get private Method
        Method say = bossClass.getDeclaredMethod("say", int.class, String.class, char.class);
        say.setAccessible(true);
        System.out.println(say.invoke(o, 20, "hsq", '男'));
        //因为say方法是static方法所以o位置可以是null
        System.out.println(say.invoke(null, 200, "Java", '女'));
        //在反射中，如果方法有返回值，统一为object， 但是他的运行类型和方法定义的返回类型一致
        Object invoke = say.invoke(o, 20, "Python", 'm');
        System.out.println(invoke.getClass());

    }
}

class Boss {
    private static String name;
    public int age;

    private static String say(int n, String s, char c) {
        return n + " " + s + " " + c;
    }

    public void hi(String s) {
        System.out.println("Hello " + s);
    }
}

